package com.cslg.ac.demo.controller.pet;

import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.entity.pet.PetAdopt;
import com.cslg.ac.demo.entity.pet.PetCommon;
import com.cslg.ac.demo.entity.pet.PetFoster;
import com.cslg.ac.demo.entity.user.User;
import com.cslg.ac.demo.service.pet.PetService;
import com.cslg.ac.demo.util.communicate.ResponseUtil;
import com.cslg.ac.demo.util.integer.IntegerUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.mail.Session;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@PropertySource(value = {"classpath:pet.properties"},encoding="utf-8")
public class PetController {
    @Autowired
    PetService petService;
    @Value("${nameMax}")
    int nameMax;
    @Value("${nameMin}")
    int nameMin;
    @Value("${typeLength}")
    int typeLength;
    @Value("${descriptionLength}")
    int descriptionLength;
    @Value("${foster}")
    int fosterKind;
    @Value("${adopt}")
    int adoptKind;
    @Value("${demandLength}")
    int demandLength;


    @RequestMapping(value = "/pet/sortByAddress", method = RequestMethod.POST)
    public void sortFoster(@RequestParam int petType, @RequestParam int offset,
                           @RequestParam Double longitude,
                           @RequestParam Double latitude,
                           @RequestParam int num, PrintWriter out){
        if(offset < 1 || num < 1){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        List<Integer> ids;
        try {
            ids = petService.sortByAddress(longitude,latitude, petType, offset - 1, num);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
            return;
        }
        if(ids == null || ids.size() < 1){
            ids = null;
        }
        Map<String, Object> param = new HashMap<>(1);
        param.put("ids", ids);
        ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
    }

    @RequestMapping(value = "/pet/sortByTime", method = RequestMethod.POST)
    public void sortByTime(@RequestParam int petType, @RequestParam int offset,
                           @RequestParam int num, PrintWriter out){
        if(offset < 1 || num < 1){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        List<Integer> ids;
        try {
            ids = petService.sortByTime(petType, offset - 1, num);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
            return;
        }
        if(ids == null || ids.size() < 1){
            ids = null;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("ids", ids);
        ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
    }

    @RequestMapping(value = "/pet/getPetsByMaster")
    public void selectPetsByMaster(@RequestParam int masterId, @RequestParam int petType,
                                   @RequestParam int offset, @RequestParam int num,
                                   @RequestParam int chooseType, PrintWriter out){
        Map<String,Object> param = new HashMap<>();
        Integer[] ids;
        if(offset < 1 || num < 1){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        try {
            ids = petService.getPetsByMaster(masterId,petType,offset-1,num, chooseType);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        param.put("ids",ids);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);
    }


    @RequestMapping(value = "/pet/getInfo")
    public void selectFosterById(@RequestParam int id, @RequestParam int petType, PrintWriter out){
        Map<String,Object> param = new HashMap<>();
        PetCommon petCommon;
        PetFoster petFoster;
        PetAdopt petAdopt;
        try {
            petCommon = petService.selectPetCommon(id);
            petAdopt = petService.selectPetAdopt(id);
            petFoster = petService.selectPetFoster(id);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        if(petCommon == null){
            ResponseUtil.responseCode(out,ResponseCode.USER_NOT_EXISTS);
            return;
        }
        param.put("common",petCommon);
        if(petType == adoptKind){
            param.put("adopt",petAdopt);
        }else if(petType == fosterKind){
            param.put("foster",petFoster);
        }else {
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED, param);
    }

    @RequestMapping(value = "/pet/addAdopt",method = RequestMethod.POST)
    public void addAdopt(PrintWriter out, HttpSession session,
                         PetCommon petCommon, PetAdopt petAdopt){
        User user = (User)session.getAttribute("user");
        if(!petCommon.inserValid(nameMax,nameMin,typeLength) || !petAdopt.insertValid(descriptionLength,demandLength)){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        petCommon.setMasterId(user.getId());
        petCommon.setKind(adoptKind);
        int petId;
        try {
            petId = petService.addAdopt(petCommon,petAdopt);
        }catch (Exception e){
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        Map<String,Object> param = new HashMap<>();
        param.put("id",petId);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);

    }

    @RequestMapping(value = "/pet/addFoster",method = RequestMethod.POST)
    public void addFoster(PrintWriter out, HttpSession session,
                    PetCommon petCommon, PetFoster petFoster){
        User user = (User)session.getAttribute("user");
        if(!petCommon.inserValid(nameMax,nameMin,typeLength)){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }

        petCommon.setMasterId(user.getId());
        petCommon.setKind(fosterKind);
        int petId;
        try {
            petId = petService.addFoster(petFoster,petCommon);
        }catch (Exception e){
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        Map<String,Object> param = new HashMap<>();
        param.put("id",petId);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);

    }


    @RequestMapping(value = "/pet/delete",method = RequestMethod.POST)
    public void delete(@RequestParam Integer id, PrintWriter out, HttpSession session){
        if(id == null){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        try {
            petService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        ResponseUtil.responseCode(out,ResponseCode.REQUEST_SUCCESSED);
    }
}
